package cn.autumnorange.app.util.consumer.queue;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 死信队列
 */
@Component
public class DeadLetterMessageReceiver {
// ---------------------------业务队列常量定义-----------------------
    /** 业务交换机标识符 */
    public static final String BUSINESS_EXCHANGE_NAME = "dead-letter-business-exchange";
    /** 业务A队列 */
    public static final String BUSINESS_QUEUEA_NAME = "dead-letter-business-queuea";
    /** 业务B队列 */
    public static final String BUSINESS_QUEUEB_NAME = "dead-letter-business-queueb";

    /** 业务A队列与死信队列交换机绑定键 标识符 */
    public static final String DEAD_LETTER_QUEUEA_ROUTING_KEY =
            "dead-letter-deadletter-queuea-routingkey";
    /** 业务B队列与死信队列交换机绑定键 标识符 */
    public static final String DEAD_LETTER_QUEUEB_ROUTING_KEY =
            "dead-letter-deadletter-queueb-routingkey";
    /** 业务A死信队列名称 */
    public static final String DEAD_LETTER_QUEUEA_NAME = "dead-letter-deadletter-queuea";
    /** 业务B死信队列名称 */
    public static final String DEAD_LETTER_QUEUEB_NAME = "dead-letter-deadletter-queueb";
    // ---------------------------订单死信队列常量定义-----------------------
    /** 订单死信队列交换机标识符 属性值不能改 写死 */
    private static final String ORDER_DEAD_LETTER_QUEUE_KEY = "x-dead-letter-exchange";

    /** 订单死信队列交换机绑定键 标识符 属性值不能改，写死 */
    private static final String ORDER_DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

    @RabbitListener(queues = DEAD_LETTER_QUEUEA_NAME)
    public void receiveA(Message message, Channel channel) throws IOException {
        System.out.println("收到死信消息A：" + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(queues = DEAD_LETTER_QUEUEB_NAME)
    public void receiveB(Message message, Channel channel) throws IOException {
        System.out.println("收到死信消息B：" + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    @RabbitListener(queues = "deadroutingqueueA")
    public void routingdeadletterqueueaReceiveB(Message message, Channel channel) throws IOException {
        System.out.println("路由死信A：" + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(queues = "deadroutingqueueB")
    public void deadLetterroutingQueueBReceiveB(Message message, Channel channel) throws IOException {
        System.out.println("路由死信B：" + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


//    @RabbitListener(queues = "deadtopicQueueA")
//    public void topicdead(Message message, Channel channel) throws IOException {
//        System.out.println("主题死信A：" + new String(message.getBody()));
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//    }
}